using System;
using System.Collections.Generic;
using System.Text;
using SP.Parameter;

namespace SP.HwDeferredRS
{
    /// <summary>
    /// A simple base map feature
    /// </summary>
    public class AlphaMapFusion : AbstractFusion
    {
        public AlphaMapFusion()
        {
            AddPreprocessor(new Preprocessor("ALPHAMAP_UV", 1, 3, 1, "UV channel (1-3) for alphaMap."));
            AddPreprocessor(new Preprocessor("ALPHAMAP_CUTOUT", 0, 1, 1, "Set to 1 to enable discard pixel's alpha value lower than alphaMapCutout."));

            // param alphaMap
            AddParameter(
                new Texture2dParamter("alphaMap", "alphaMapSamp")
                .SetSRGBTexture(true)
                .SetUserData("fragment")
                .SetDesc("R: alpha; GBA: unused")
                );

            // param alphaMapCutout
            AddParameter(
                new ScalarParameter("alphaMapCutout")
                .SetS(0.05f)
                );
        }

        public override string Name
        {
            get { return "AlphaMap"; }
        }

        public override string Desc
        {
            get { return "Enable this fusion to support Cut-out effect."; }
        }

        public override Stage Stage
        {
            get { return Stage.Opacity; }
        }

        public override int UVChannelCount
        {
            get { return GetPreprocessor("ALPHAMAP_UV").Value; }
        }

        public override void Code(StringBuilder b, string platform)
        {
            AddLine(b, "vec4 alphaMap = tex2D(alphaMapSamp, pipeline.Texcoord[ALPHAMAP_UV-1]);");
            AddLine(b, "#if ALPHAMAP_CUTOUT");
            AddLine(b, "clip(alphaMap.r - alphaMapCutout);");
            AddLine(b, "#endif // ALPHAMAP_CUTOUT");
            AddLine(b, "pipeline.Opacity *= alphaMap.r;");
        }
    }
}